<?php
/**
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * This license is also available via the world-wide-web at
 * http://itdapps.ncsu.edu/bsd.txt
 *
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to itappdev@ncsu.edu so we can send you a copy immediately.
 *
 * @package    Ot_Oauth_Server_Nonce
 * @category   Model
 * @copyright  Copyright (c) 2007 NC State University Office of Information Technology
 * @license    http://itdapps.ncsu.edu/bsd.txt  BSD License
 * @version    SVN: $Id: $
 */

/**
 *
 * @package    Ot_Oauth_Server_Nonce
 * @category   Model
 * @copyright  Copyright (c) 2007 NC State University Office of Information Technology
 *
 */
class Ot_Oauth_Server_Nonce extends Ot_Db_Table
{
    /**
     * Name of the table in the database
     *
     * @var string
     */
    protected $_name = 'tbl_ot_oauth_server_nonce';

    /**
     * Primary key of the table
     *
     * @var string
     */
    protected $_primary = 'nonceId';
    
    public function getNonceByConsumerAndToken($consumerId, $token)
    {
    	$where = $this->getAdapter()->quoteInto('consumerId = ?', $consumerId)
    	       . ' AND '
    	       . $this->getAdapter()->quoteInto('token = ?', $token);
    	
    	$result = $this->fetchAll($where, 'timestamp desc', 1);
    	
    	if ($result->count() != 1) {
    		return null;
    	}
    	
    	return $result->current();
    }    
    
    public function deleteOldNonce($consumerId, $token, $timestamp)
    {
    	$where = $this->getAdapter()->quoteInto('consumerId = ?', $consumerId)
    	       . ' AND '
    	       . $this->getAdapter()->quoteInto('token = ?', $token)
    	       . ' AND '
    	       . $this->getAdapter()->quoteInto('timestamp < ?', (int)$timestamp);
    	       
    	$this->delete($where);
    }
}